using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace Vipesolutions.SharePoint.AddWebpartPickerExtension
{
    class AddWebpartPickerExtensionFeature : SPFeatureReceiver
    {
        private const string WebConfigModificationOwner = "Vipesolutios.Sharepoint.AddWebpartPickerModule";

        private static readonly SPWebConfigModification[] Modifications = {
            new SPWebConfigModification()
                { 
                    Owner = WebConfigModificationOwner, 
                    Name = "add[@name='Vipesolutios.Sharepoint.AddWebpartPickerModule']",
                    Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, 
                    Path = "configuration/system.web/httpModules",
                    Sequence = 0,
                    Value = "<add name=\"Vipesolutios.Sharepoint.AddWebpartPickerModule\" type=\"Vipesolutions.SharePoint.AddWebpartPickerExtension.HttpModule.AddWebPartPickerInjectHttpModule, Vipesolutions.SharePoint.AddWebpartPickerExtension, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b3436f13dfaf74a7\" />" 
                }
        };

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            if (webApp != null)
                AddWebConfigModifications(webApp, Modifications);
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            if (webApp != null)
                RemoveWebConfigModificationsByOwner(webApp, WebConfigModificationOwner);
        }

        private void AddWebConfigModifications(SPWebApplication webApp, IEnumerable<SPWebConfigModification> modifications)
        {
            foreach (SPWebConfigModification modification in modifications)
                webApp.WebConfigModifications.Add(modification);

            webApp.Update();
            webApp.WebService.ApplyWebConfigModifications();
        }

        private void RemoveWebConfigModificationsByOwner(SPWebApplication webApp, string owner)
        {
            Collection<SPWebConfigModification> modificationCollection = webApp.WebConfigModifications;
            Collection<SPWebConfigModification> removeCollection = new Collection<SPWebConfigModification>();

            int count = modificationCollection.Count;

            for (int i = 0; i < count; i++)
            {
                SPWebConfigModification modification = modificationCollection[i];

                if (modification.Owner == owner)
                    removeCollection.Add(modification);
            }

            if (removeCollection.Count > 0)
            {
                foreach (SPWebConfigModification modificationItem in removeCollection)
                    webApp.WebConfigModifications.Remove(modificationItem);

                webApp.Update();
                webApp.WebService.ApplyWebConfigModifications();
            }
        }
    }
}